<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>权限管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<script src="../js/global.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
			<legend>系統功能权限维护</legend>
		</fieldset>
		<div class="layui-container">
			<div class="layui-row">
				<div id="moduleTreeContainer" class="layui-col-md3">
					<div id="moduleTree" class="demo-tree demo-tree-box" style="width: 100%; height:100%; ">
					</div>
				</div>
				<div class="layui-col-md9">
					<div id="divModuleB">
						<button id="btnAddChild" type="button" class="layui-btn layui-btn-sm">添加权限</button>
						<!-- <button id="btnChangeParent" type="button" class="layui-btn layui-btn-normal layui-btn-sm">设置父节点</button> -->
						<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
							<legend><span id="spTitle">功能权限信息维护</span></legend>
						</fieldset>
						<form id="formSave" class="layui-form" action="" onsubmit="return false">
							<div class="layui-form-item">
								<label class="layui-form-label">所属模块</label>
								<div class="layui-input-block">
									  <select id="permission_module" name="module" lay-verify="required" disabled="disabled">
										<option value="">请选择模块</option>
										
									  </select>
									<!-- <input id="txtParentId" type="text" disabled="disabled" value="0" class="layui-input">
									<input id="hfParentId" type="hidden" name="module"> -->
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">权限编号</label>
								<div class="layui-input-block">
									<input id="hfId" type="hidden" name="mId">
									<input id="txtId" type="text" disabled="disabled" value="0" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">权限名称</label>
								<div class="layui-input-block">
									<input id="txtName" type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入权限名称" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">权限值</label>
								<div class="layui-input-block">
									<input id="txtValue" type="text" name="value" lay-verify="required" placeholder="请输入权限值如查询角色(role:query)" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit="" lay-filter="subData">保存</button>
									<button type="reset" class="layui-btn layui-btn-primary">重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script>
		layui.use(['tree','form','layer'],function(){
			var tree=layui.tree,
			form=layui.form,
			layer=layui.layer,
			$=layui.$;
				//渲染模块下拉框
				$.ajax({
					url:globalData.permissionServer+"/module/findAllModules",//请求地址
					type:'get',//请求方法，默认为 get
					data:{},//需要传递到服务端的数据
					dataType:'json',//服务端响应数据类型
					//contentType: "application/json",
					xhrFields: {
						withCredentials: true
					},
					// beforeSend: function(request) {
					// 	request.setRequestHeader("token", globalData.getCurToken());
					// },
					success:function(res){
						
						var html="";
						console.log(res);
						if(res.code==0){
							var data=res.data;
							for(var i=0;i<data.length;i++){
								html+='<option value="'+data[i].moduleName+'">'+data[i].moduleName+'</option>';
							}
							$("#permission_module").append(html);
							//刷新select选择框渲染
							form.render('select');
						}else{
							layer.msg(res.msg);
						}
					}
				});
			
			
				$.ajax({
					type : "get",
					url : globalData.permissionServer+"/permission/listPermissions",
					data:{},//需要传递到服务端的数据
					dataType:'json',//服务端响应数据类型
					xhrFields: {
						withCredentials: true
					},
					// beforeSend: function(request) {
					// 	request.setRequestHeader("token", globalData.getCurToken());
					// },
					success : function(res) {
						
						//渲染模块树	
						tree.render({
							id:'allModule',
							elem:'#moduleTree',
							data:res,
							edit:['del'],
							click:function(obj){
								var data=obj.data;
								var elem=obj.elem;
								//如果是权限节点
								if(data.field == 'permission'){
									
									if($("#permission_module").attr("disabled")){
										$("#permission_module").removeAttr("disabled");
									}
									
									var pid=0;
									//查询点击节点的权限信息
									$.ajax({
										type : "get",
										url : globalData.permissionServer+"/permission/getPermissionByPermissionId",
										data:{
											permissionId : data.id
										},//需要传递到服务端的数据
										dataType:'json',//服务端响应数据类型
										xhrFields: {
											withCredentials: true
										},
										// beforeSend: function(request) {
										// 	request.setRequestHeader("token", globalData.getCurToken());
										// },
										success : function(res) {
												if(res.code==0){
													var permission=res.data;
													//var rs=JSON.parse(res.message);
													//获取信息写入数据
													//$("#permission_module").removeAttr("disabled");
													var permissionModule=permission.permissionModule;
													$("#permission_module option[value='"+permissionModule+"']").attr("selected","selected");
													//刷新select选择框渲染
													form.render('select');
													$("#txtId").val(permission.permissionId);
													$("#txtName").val(permission.permissionName);
													$("#txtValue").val(permission.permissionValue);
													//pid=res.parent_id;
												}else{
													layer.alert(res.msg,{icon:2,time:1500});
													
													return ;
												}
												$("#spTitle").html('功能权限信息维护');
												
										}
									});
								}else{
									layer.msg(data.title);
								}
								
							},operate:function(obj){
								var type=obj.type;
								var data=obj.data;
								if(type==='del'){
									if(data.field=='permission'){
										layer.confirm('确定要删除['+data.title+']吗?',{icon:3},function(index){
											layer.close(index);
											$.ajax({
												type : "delete",
												url : globalData.permissionServer+"/permission/deletePermission",
												data:{
													'permissionId':data.id,
												},//需要传递到服务端的数据
												dataType:'json',//服务端响应数据类型
												xhrFields: {
													withCredentials: true
												},
												// beforeSend: function(request) {
												// 	request.setRequestHeader("token", globalData.getCurToken());
												// },
												success : function(res) {
													if(res.code==0){
														layer.msg(res.msg,{icon:1,time:1500},function(index){
															//重载树结构
															active.reload();
														});
													}else{
														layer.msg(res.msg,{icon:2,time:1500},function(){
															//重载树结构
															active.reload();
														});
													}
												}
											});
											
										},function(){
											//重载树结构
											active.reload();
										});
									}else{
										layer.msg('该页面不允许删除模块！',{icon:2,time:2000},function(){
											//重载树结构
											active.reload();
										});
									}
								}
							}
						});
					}
				});
			
			//添加权限事件
			$("#btnAddChild").click(function(){
				$("#spTitle").html('添加权限节点');
				
				$("#permission_module").removeAttr("disabled");
				//刷新select选择框渲染
				form.render('select');
				//$("#permission_module").val('');
				//清空输入框
				$("#txtId").val(0);
				$("#txtName").val('');
				$("#txtValue").val('');
			});
			form.on('submit(subData)',function(obj){
				if($("#txtId").val()==0){
					active.addModule();
					
				}else{
					active.updateModule();
				}
			});
			
			
			var	active={
					updateModule:function(){
						
						$.ajax({
							type : "put",
							url : globalData.permissionServer+"/permission/updatePermission",
							data:{
								'permissionId':$("#txtId").val(),
								'permissionModule':$("#permission_module").val(),
								'permissionName':$("#txtName").val(),
								'permissionValue':$("#txtValue").val()
							},
							dataType:'json',
							xhrFields: {
								withCredentials: true
							},
							// beforeSend: function(request) {
							// 	request.setRequestHeader("token", globalData.getCurToken());
							// },
							success : function(res) {
								if(res.code==0){
									layer.msg(res.msg,{icon:1,time:2000},function(inex){
										layer.close(inex);
										active.reload();
									});
								}else{
									layer.msg(res.msg,{icon:2,time:2000});
								}
							}
						});
						
					},
					addModule:function(){
						var permission_module=$("#permission_module").val();
						if(permission_module!=""&&permission_module!=undefined){
							$.ajax({
								type : "post",
								url : globalData.permissionServer+"/permission/insertPermission",
								data:{
									'permissionModule':permission_module,
									'permissionName':$("#txtName").val(),
									'permissionValue':$("#txtValue").val()
								},//需要传递到服务端的数据
								dataType:'json',//服务端响应数据类型
								xhrFields: {
									withCredentials: true
								},
								// beforeSend: function(request) {
								// 	request.setRequestHeader("token", globalData.getCurToken());
								// },
								success : function(res) {
									if(res.code==0){
										layer.msg(res.msg,{icon:1,time:2000},function(inex){
											layer.close(inex);
											active.reload();
										});
									}else{
										layer.msg(res.msg,{icon:2,time:2000});
									}
								}
							});
						}else{
							layer.msg('请选择所属模块',{icon:2,time:1500});
						}
						
					},
					//重载树组件
					reload:function(){
						$.ajax({
							type : "get",
							url : globalData.permissionServer+"/permission/listPermissions",
							data:{},//需要传递到服务端的数据
							dataType:'json',//服务端响应数据类型
							xhrFields: {
								withCredentials: true
							},
							// beforeSend: function(request) {
							// 	request.setRequestHeader("token", globalData.getCurToken());
							// },
							success : function(res) {
								//重载树结构
								tree.reload('allModule',{
									data:res
								});
							}
						});
					}
				}
		});
	
	</script>
</html>